﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using qlHieuThuocDongY.BusinessLogic;
using qlHieuThuocDongY.BusinessObject;
namespace qlHieuThuocDongY
{
    public partial class frmDanhMucThuoc : Form
    {
        private DanhMucThuocBUS dmThuocBUS = new DanhMucThuocBUS();
        public frmDanhMucThuoc()
        {
            InitializeComponent();
        }

        private void frmThongTinThuoc_Load(object sender, EventArgs e)
        {
           
            // Load DataGridView
            dataGridViewDanhMucThuoc.DataSource = dmThuocBUS.LayDanhMucThuoc();
            EnableEditing(false);
        }

        private void dataGridViewDanhMucThuoc_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            // dòng hiện tại
            int dong = e.RowIndex;
            // thực thi binding data lên control, khi click vào row dataGridView
            txtMaThuoc.Text = dataGridViewDanhMucThuoc.Rows[dong].Cells["MaThuoc"].Value.ToString();
            txtTenThuoc.Text = dataGridViewDanhMucThuoc.Rows[dong].Cells["TenThuoc"].Value.ToString();
            txtGia.Text = dataGridViewDanhMucThuoc.Rows[dong].Cells["Gia"].Value.ToString();
            
        }

        private void EnableEditing(bool editing)
        {
            //button
            btnThem.Enabled = !editing;
            btnSua.Enabled = !editing;
            btnXoa.Enabled = !editing;
            btnLuu.Enabled = editing;
            btnKhongLuu.Enabled = editing;
            // texBox,Combobox
            txtTenThuoc.Enabled = editing;
            txtMaThuoc.Enabled = editing;
            txtGia.Enabled = editing;
           
            // dataGriView
            dataGridViewDanhMucThuoc.Enabled = !editing;
        }

        private void ResetTextValue()
        {
            txtTenThuoc.Text = "";
            txtGia.Text = "";
        }

        private void btnThem_Click(object sender, EventArgs e)
        {

            EnableEditing(true);
            ResetTextValue();
          //  DanhMucThuoc dmthuoc= new DanhMucThuoc();
            txtMaThuoc.Text = dmThuocBUS.NextID();
        }

        private void btnKhongLuu_Click(object sender, EventArgs e)
        {
            EnableEditing(false);
        }

        private DanhMucThuoc LayDMThuoc()
        {
            DanhMucThuoc dmThuoc = new DanhMucThuoc();
            dmThuoc.MaThuoc = txtMaThuoc.Text;
            dmThuoc.TenThuoc = txtTenThuoc.Text;
            dmThuoc.Gia = Convert.ToDouble(txtGia.Text);
   
            return dmThuoc;
        }
        private void btnLuu_Click(object sender, EventArgs e)
        {

            DanhMucThuoc dmThuoc = LayDMThuoc();
            // Nếu tồn tại mã bệnh nhân rồi thì là sửa
            if (dmThuocBUS.CheckExists(dmThuoc.MaThuoc))
            {
                if (dmThuocBUS.SuaThuoc(dmThuoc))
                    frmThongTinThuoc_Load(sender, e);
            }
            else // Thêm
            {
                if (dmThuocBUS.ThemDMThuoc(dmThuoc))
                    frmThongTinThuoc_Load(sender, e);
            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            EnableEditing(true);
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn Có Muốn Xoá Thuốc: " + txtTenThuoc.Text + "Không?", "Hỏi",
               MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                if (dmThuocBUS.XoaThuoc(txtMaThuoc.Text))
                    // load lại form
                    frmThongTinThuoc_Load(sender, e);
        }
    }
}
